<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
     /**
      * 自定义验证规则
      */
     function validateForm(){
         form.bootstrapValidator({
             message: "无效的值",
             feedbackIcons: {
                 valid: "glyphicon glyphicon-ok",
                 invalid: "glyphicon glyphicon-remove",
                 validating: "glyphicon glyphicon-refresh"
             },
             fields: {
                 imageInfo_1: {
                     validators: {
                         notEmpty: {message: "请上传图片"},
                         file:{maxSize: 500*500, message: '请上传一张最大不超过1M的jpg/png/gif图片文件'}
                     }
                 },
                 userName: {
                     validators: {
                         notEmpty: {message: "管理员名称不可为空"},
                         stringLength: {max: 50,message: "管理员名称最大长度为50"},
                         threshold :  1 ,
                         /**bootstrap validator 远程校验需要返回{"valid",true or false}**/
                         remote: {
                             url:'${basePath}/back/users/checkUsers.sjson',
                             message:  '管理员已存在',
                             delay:  20000,
                             type:  'POST',
                             data: function(validator) {
                                 return {
                                     userName:$("#userName").val(),
                                     usersId:$("#usersId").val()
                                 };
                             }
                         }
                     }
                 },
                 password: {
                     validators: {
                         notEmpty: {message: "密码不可为空"},
                         stringLength: {min: 6,max: 20,message: "密码长度为6至20"}
                     }
                 },
                 trueName: {
                     validators: {
                         notEmpty: {message: "真实姓名不可为空"},
                         stringLength: {max: 20, message: "真实姓名最大长度为20"},
                         regexp: {
                             regexp: validateUtil.chinese,
                             message: '真实姓名为汉字'
                         }
                     }
                 },
                 email: {
                     validators: {
                         notEmpty: {message: "电子邮件不可为空"},
                         stringLength: {max: 50,message: "电子邮件最大长度为50"},
                         regexp: {
                             regexp: validateUtil.email,
                             message: '请输入有效的电子邮件'
                         }
                     }
                 },
                 phone: {
                     validators: {
                         notEmpty: {message: "手机号不可为空"},
                         regexp: {
                             regexp:  validateUtil.phone,
                             message: '请输入有效的手机号'
                         }
                     }
                 },
                 comments: {
                     validators: {
                         stringLength: {max: 500,message: "备注最大长度为500"}
                     }
                 }
             }
         });
     }

     /**
      * 添加管理员名
      */
	 <shiro:hasPermission name="back_gotoUsersPage_add">
     function addInfo(){
         $("#myModalLabel").empty().text("添加管理员信息");
         /**清空隐藏域的值**/
         $("#usersId").val("");
         $("#imageUrl_1").val("");
         $("#registerDate").val("");
         /**如果有上传图片功能添加此代码，否则可以去掉**/
         initImageForAdd(1,"sys_users","image_users",1,500,500);
         resetAddForm();
     }
	 </shiro:hasPermission>

     /**
      * 编辑管理员名
      */
	 <shiro:hasPermission name="back_gotoUsersPage_update">
     function editInfo(){
         $("#myModalLabel").empty().text("修改管理员信息");
         /**获取选中行的id**/
         var selectedRow= $("#tt").bootstrapTable('getSelections');
         if(selectedRow.length==1){
             var id=selectedRow[0].usersId;
             $("#btn_edit").attr("data-target","#addOrEditModal");/**选中了记录后才可打开对应的模态框**/
             $.ajax({
                 type: "POST",
                 dataType: "JSON",
                 url: "${basePath}/back/users/getUsersObject.sjson",
                 data: {usersId:id},
                 success: function(result){
                     var data = JSON.parse(result.data);
                     /**隐藏域**/
                     $("#usersId").val(data.usersId);
                     if(data.registerDate!=undefined&&data.registerDate!="") {
                         $("#registerDate").val(new Date(data.registerDate).Format("yyyy-MM-dd hh:mm:ss"));
                     }
                     $("#imageUrl_1").val(data.userPhoto);
                     /**可见表单控件**/
                     $("#userName").val(data.userName);
                     $("#password").val(data.password);
                     $("#trueName").val(data.trueName);
                     $("#email").val(data.email);
                     $("#phone").val(data.phone);
                     $("#isShow_"+data.lockState).attr("checked","checked");
                     $("#comments").val(data.comments);

                     /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
                     initImageForUpdate(1,"sys_users","image_users",1,500,500,"${uploadPath}",data.userPhoto);
                     reValidateForm();
                     removeValidateField('imageInfo_1');
                 }
             });
         }else{
             $("#btn_edit").attr("data-target",""); /**没有选中记录不可打开模态框**/
             showMsg("请选择一行记录！");
         }
     }
	 </shiro:hasPermission>

</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button"  class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1"  method="post" action="${basePath}/back/users/saveOrUpdateUsers.sjson">
					<input id="usersId" type="hidden" name="usersId"/>
					<input id="registerDate" type="hidden" name="registerDate"/>

					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>管理员头像:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="container">
									<input id="imageUrl_1" type="hidden" name="userPhoto" value=""/>
									<div class="form-group" id="fileWrap1"></div>
								</div>
								<span style="color:red">提示：请上传规格宽500px，高500px的图片</span>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>管理员名称:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="userName" name="userName" placeholder="请输入管理员名称"/>
								</div>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>密码:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input type="password" class="form-control input-sm" id="password" name="password" placeholder="请输入密码"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>真实姓名:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="trueName" name="trueName" placeholder="请输入真实姓名"/>
								</div>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>电子邮件:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="email" name="email" placeholder="请输入电子邮件"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>手机号码:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="phone" name="phone" placeholder="请输入手机号码"/>
								</div>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>冻结状态:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<label class="checkbox-inline">
									<input type="radio" name="lockState" id="isShow_1" class="radioInput" value="1">&nbsp;已冻结
								</label>
								<label class="checkbox-inline">
									<input type="radio" name="lockState" id="isShow_0" class="radioInput" value="0"  checked>&nbsp;未冻结
								</label>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px">备注:&nbsp;&nbsp;</td>
							<td  class="toleft_td" colspan="3">
								<div class="form-group">
									<textarea class="form-control input-sm" type="text" id="comments" name="comments" placeholder="请输入备注"></textarea>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary"  onclick="submitFormByRemote()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
